/*
 * APICloud JavaScript Library
 * Copyright (c) 2014 apicloud.com
 */
(function(window) {
	var u = {};
	var uzStorage = function() {
		var ls = window.localStorage;
		return ls;
	};
	u.TimeDiff = function(sDate) { //sDate1和sDate2是2006-12-18格式  
		var oDate1, oDate2, iDays
		oDate1 = new Date() //转换为12-18-2006格式   
		oDate2 = new Date(sDate)
		iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
		return iDays
	};
	u.TimeCompare = function(sDate, eDate) { //sDate1和sDate2是2006-12-18格式  
		var oDate1, oDate2, iDays
		oDate1 = new Date(sDate)

		oDate2 = new Date(eDate)
		iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
		return iDays
	};

	u.DateDiff = function(sDate) { //sDate1和sDate2是2006-12-18格式  
		var oDate1, oDate2, iDays
		oDate1 = new Date() //转换为12-18-2006格式  
		aDate = sDate.split("-")
		oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
		iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
		return iDays
	};
	u.formatCTime = function(date) { 
		var time = date.replace("T", " ");
		var index = time.indexOf(".");
		if(index > 0)
			time = time.substr(0, index);
		return time;
	};
	u.DateCompare = function(sDate, eDate) { //sDate1和sDate2是2006-12-18格式  
		var oDate1, oDate2, iDays
		//oDate1 = new Date() //转换为12-18-2006格式  
		aDate = sDate.split("-")
		oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])

		aDate = eDate.split("-")
		oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
		iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数  
		return iDays
	};

	function parseArguments(url, data, fnSuc, dataType) {
		if(typeof(data) == 'function') {
			dataType = fnSuc;
			fnSuc = data;
			data = undefined;
		}
		if(typeof(fnSuc) != 'function') {
			dataType = fnSuc;
			fnSuc = undefined;
		}
		return {
			url: url,
			data: data,
			fnSuc: fnSuc,
			dataType: dataType
		};
	}

	var wait = 120; //时间
	u.time = function(obj) {
		if(wait == 0) {
			$(obj).attr("disabled", false);
			$(obj).text("获取验证码"); //改变按钮中value的值 
			wait = 120;
		} else {
			$(obj).attr("disabled", true); //倒计时过程中禁止点击按钮
			$(obj).text(wait + "秒后重发"); //改变按钮中value的值
			wait--;
			setTimeout(function() {
					u.time(obj); //循环调用
				},
				1000);
		}
	};
	u.formatDay = function(val) {
		if(!val) return "";

		var re = /-?\d+/;
		var m = re.exec(val);
		var d = new Date(parseInt(m[0]));
		// 按【2012-02-13】的格式返回日期    
		return d.format("yyyy-MM-dd");
	};

	function rmoney(s) {
		return parseFloat(s.replace(/[^\d\.-]/g, ""));
	}

	//货币格式
	u.toMoney = function(num) {
		if(num == null || num == "" || num == undefined) {
			return "0.00";
		} else {
			num = num.toString().replace(/\$|\,/g, '');
			if(isNaN(num))
				num = "0";
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num * 100 + 0.50000000001);
			cents = num % 100;
			num = Math.floor(num / 100).toString();
			if(cents < 10)
				cents = "0" + cents;
			for(var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
				num = num.substring(0, num.length - (4 * i + 3)) + ',' +
				num.substring(num.length - (4 * i + 3));

			return(((sign) ? '' : '-') + num + '.' + cents);
		}
	}
	//日期格式化
	u.formatTime = function(val) {
		if(!val) return "";

		var re = /-?\d+/;
		var m = re.exec(val);
		var d = new Date(parseInt(m[0]));
		// 按【2012-02-13 09:09:09】的格式返回日期    
		return d.format("yyyy-MM-dd hh:mm:ss");
	};

	u.ajaxget = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: '../../img/icon/loading.gif'
		});
		$.ajax({
			type: 'get',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					top.$.ligerDialog.warn(obj.Message, function() {
						parent.logout();
					});
					return;
				}
				callback(obj);
			},
			error: function(data) {
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});

	};

	u.ajaxall = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'img/icon/loading.gif'
		});
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				callback(obj);
			},
			error: function(data) {
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});

	};
	u.ajaxarr = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'https://media.zgj.zhelan.net/loading.gif'
		});
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			traditional: false,
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					top.$.ligerDialog.warn(obj.Message, function() {
						parent.logout();
					});
				} else if(obj.Code == 0)
					callback(obj);
				else
					u.showtoast(obj.Message);
			},
			error: function(data) {
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});
	};
	u.ajax2 = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'https://media.zgj.zhelan.net/loading.gif'
		});
		if(url.indexOf("token")<0){
			if(url.indexOf("?")>0){
				url += "&token=" +  u.getStorage("token");
			}
			else
				url +="?token=" +  u.getStorage("token");
		}
		
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					top.$.ligerDialog.warn(obj.Message, function() {
						parent.logout();
					});
				} else if(obj.Code == 0)
					callback(obj);
				else
					top.$.ligerDialog.warn(obj.Message);

			},
			error: function(data) {
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});
	};
	u.ajax = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'https://media.zgj.zhelan.net/loading.gif'
		});
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					$api.showLToast(obj.Message, 3000);
					window.location.href = "syslogin.html";

				} else if(obj.Code == 0)
					callback(obj);
				else
					$api.showLToast(obj.Message, 3000);

			},
			error: function(data) {
				console.log(data);
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});
	};

	u.ajaxapp2 = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'https://media.zgj.zhelan.net/loading.gif'
		});
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					top.$.ligerDialog.warn(obj.Message, function() {
						parent.logout();
					});
				} else if(obj.Code == 0)
					callback(obj);
				else
					top.$.ligerDialog.warn(obj.Message);

			},
			error: function(data) {
				console.log(data);
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});
	};

	u.ajaxapp = function(url, data, callback) {
		var infoDialog = $.dialog({
			type: 'info',
			infoText: 'loading…',
			infoIcon: 'https://media.zgj.zhelan.net/loading.gif'
		});
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			async: true,
			dataType: 'json',
			success: function(obj) {
				infoDialog.dialog.close();
				if(obj.Code == 100) {
					u.clearStorage();
					top.$.ligerDialog.warn(obj.Message, function() {
						window.location.href = "index.html";
					});
				} else if(obj.Code == 0)
					callback(obj);
				else
					top.$.ligerDialog.warn(obj.Message);

			},
			error: function(data) {
				console.log(data);
				infoDialog.dialog.close();
				u.showtoast("系统繁忙，请重试！");
			}
		});
	};
	u.isMobile = function(obj) {
		var myreg = /^[1][3,4,5,7,8,6,9][0-9]{9}$/;
		if(!myreg.test(obj)) {
			return false;
		}
		return true;
	};
	u.isEmail = function(obj) {
		var myreg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
		if(!myreg.test(obj)) {
			return false;
		}
		return true;
	};
	u.Rad = function(d) {
		//经纬度转换成三角函数中度分表形式。
		return d * Math.PI / 180.0;
	};
	u.getDistance = function(lat1, lng1, lat2, lng2) {
		//传入两个点的经纬度
		var radLat1 = Rad(lat1);
		var radLat2 = Rad(lat2);
		var a = radLat1 - radLat2;
		var b = Rad(lng1) - Rad(lng2);
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
			Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * 6378.137; // EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000; //输出为公里
		s = s.toFixed(2); //保留小数点后两位小数
		return s;
	};
	u.is_weixin = function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			return true;
		}
		return false;

	};
	u.arrcontain = function(arr, val) {
		if(arr.length == 0) return false;
		for(var i in arr) {
			if(arr[i] == val) return true;

		}
		return false;
	};
	u.isEmpty = function(item) {
		return(item == null || item == 'undefined' || item == undefined || item == '' || item == 'null');
	};
	u.compareTime = function(str) {
		var curr = new Date();
		var date = new Date(str);
		return curr.getTime() < date.getTime();
	}
	u.formatTemplate = function(data, tmpl) {
		return tmpl.replace(/{(\w+)}/g, function(m1, m2) {
			if(!m2)
				return '';
			return data[m2];
		});
	};

	u.getparastr = function(strname) {
		var hrefstr, pos, parastr, para, tempstr;
		hrefstr = window.location.href;
		pos = hrefstr.indexOf("?")
		parastr = hrefstr.substring(pos + 1);
		para = parastr.split("&");
		tempstr = "";
		for(i = 0; i < para.length; i++) {
			tempstr = para[i];
			pos = tempstr.indexOf("=");
			if(tempstr.substring(0, pos) == strname) {
				return tempstr.substring(pos + 1);
			}
		}
		return null;
	};

	u.toMoney = function(num) {
		if(num == null || num == "" || num == undefined) {
			return "0.00";
		} else {
			num = num.toString().replace(/\$|\,/g, '');
			if(isNaN(num))
				num = "0";
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num * 100 + 0.50000000001);
			cents = num % 100;
			num = Math.floor(num / 100).toString();
			if(cents < 10)
				cents = "0" + cents;
			for(var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
				num = num.substring(0, num.length - (4 * i + 3)) + ',' +
				num.substring(num.length - (4 * i + 3));

			return(((sign) ? '' : '-') + num + '.' + cents);
		}
	};
	u.trim = function(str) {
		if(String.prototype.trim) {
			return str == null ? "" : String.prototype.trim.call(str);
		} else {
			return str.replace(/(^\s*)|(\s*$)/g, "");
		}
	};
	u.trimAll = function(str) {
		return str.replace(/\s*/g, '');
	};
	u.isElement = function(obj) {
		return !!(obj && obj.nodeType == 1);
	};
	u.isArray = function(obj) {
		if(Array.isArray) {
			return Array.isArray(obj);
		} else {
			return obj instanceof Array;
		}
	};
	u.isEmptyObject = function(obj) {
		if(JSON.stringify(obj) === '{}') {
			return true;
		}
		return false;
	};
	u.addEvt = function(el, name, fn, useCapture) {
		if(!u.isElement(el)) {
			console.warn('$api.addEvt Function need el param, el param must be DOM Element');
			return;
		}
		useCapture = useCapture || false;
		if(el.addEventListener) {
			el.addEventListener(name, fn, useCapture);
		}
	};
	u.rmEvt = function(el, name, fn, useCapture) {
		if(!u.isElement(el)) {
			console.warn('$api.rmEvt Function need el param, el param must be DOM Element');
			return;
		}
		useCapture = useCapture || false;
		if(el.removeEventListener) {
			el.removeEventListener(name, fn, useCapture);
		}
	};
	u.one = function(el, name, fn, useCapture) {
		if(!u.isElement(el)) {
			console.warn('$api.one Function need el param, el param must be DOM Element');
			return;
		}
		useCapture = useCapture || false;
		var that = this;
		var cb = function() {
			fn && fn();
			that.rmEvt(el, name, cb, useCapture);
		};
		that.addEvt(el, name, cb, useCapture);
	};
	u.dom = function(el, selector) {
		if(arguments.length === 1 && typeof arguments[0] == 'string') {
			if(document.querySelector) {
				return document.querySelector(arguments[0]);
			}
		} else if(arguments.length === 2) {
			if(el.querySelector) {
				return el.querySelector(selector);
			}
		}
	};
	u.domAll = function(el, selector) {
		if(arguments.length === 1 && typeof arguments[0] == 'string') {
			if(document.querySelectorAll) {
				return document.querySelectorAll(arguments[0]);
			}
		} else if(arguments.length === 2) {
			if(el.querySelectorAll) {
				return el.querySelectorAll(selector);
			}
		}
	};
	u.byId = function(id) {
		return document.getElementById(id);
	};
	u.first = function(el, selector) {
		if(arguments.length === 1) {
			if(!u.isElement(el)) {
				console.warn('$api.first Function need el param, el param must be DOM Element');
				return;
			}
			return el.children[0];
		}
		if(arguments.length === 2) {
			return this.dom(el, selector + ':first-child');
		}
	};
	u.last = function(el, selector) {
		if(arguments.length === 1) {
			if(!u.isElement(el)) {
				console.warn('$api.last Function need el param, el param must be DOM Element');
				return;
			}
			var children = el.children;
			return children[children.length - 1];
		}
		if(arguments.length === 2) {
			return this.dom(el, selector + ':last-child');
		}
	};
	u.eq = function(el, index) {
		return this.dom(el, ':nth-child(' + index + ')');
	};
	u.not = function(el, selector) {
		return this.domAll(el, ':not(' + selector + ')');
	};
	u.prev = function(el) {
		if(!u.isElement(el)) {
			console.warn('$api.prev Function need el param, el param must be DOM Element');
			return;
		}
		var node = el.previousSibling;
		if(node.nodeType && node.nodeType === 3) {
			node = node.previousSibling;
			return node;
		}
	};
	u.next = function(el) {
		if(!u.isElement(el)) {
			console.warn('$api.next Function need el param, el param must be DOM Element');
			return;
		}
		var node = el.nextSibling;
		if(node.nodeType && node.nodeType === 3) {
			node = node.nextSibling;
			return node;
		}
	};
	u.closest = function(el, selector) {
		if(!u.isElement(el)) {
			console.warn('$api.closest Function need el param, el param must be DOM Element');
			return;
		}
		var doms, targetDom;
		var isSame = function(doms, el) {
			var i = 0,
				len = doms.length;
			for(i; i < len; i++) {
				if(doms[i].isEqualNode(el)) {
					return doms[i];
				}
			}
			return false;
		};
		var traversal = function(el, selector) {
			doms = u.domAll(el.parentNode, selector);
			targetDom = isSame(doms, el);
			while(!targetDom) {
				el = el.parentNode;
				if(el != null && el.nodeType == el.DOCUMENT_NODE) {
					return false;
				}
				traversal(el, selector);
			}

			return targetDom;
		};

		return traversal(el, selector);
	};
	u.contains = function(parent, el) {
		var mark = false;
		if(el === parent) {
			mark = true;
			return mark;
		} else {
			do {
				el = el.parentNode;
				if(el === parent) {
					mark = true;
					return mark;
				}
			} while (el === document.body || el === document.documentElement);

			return mark;
		}

	};
	u.remove = function(el) {
		if(el && el.parentNode) {
			el.parentNode.removeChild(el);
		}
	};
	u.attr = function(el, name, value) {
		if(!u.isElement(el)) {
			console.warn('$api.attr Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length == 2) {
			return el.getAttribute(name);
		} else if(arguments.length == 3) {
			el.setAttribute(name, value);
			return el;
		}
	};
	u.removeAttr = function(el, name) {
		if(!u.isElement(el)) {
			console.warn('$api.removeAttr Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length === 2) {
			el.removeAttribute(name);
		}
	};
	u.hasCls = function(el, cls) {
		if(!u.isElement(el)) {
			console.warn('$api.hasCls Function need el param, el param must be DOM Element');
			return;
		}
		if(el.className.indexOf(cls) > -1) {
			return true;
		} else {
			return false;
		}
	};
	u.addCls = function(el, cls) {
		if(!u.isElement(el)) {
			console.warn('$api.addCls Function need el param, el param must be DOM Element');
			return;
		}
		if('classList' in el) {
			el.classList.add(cls);
		} else {
			var preCls = el.className;
			var newCls = preCls + ' ' + cls;
			el.className = newCls;
		}
		return el;
	};
	u.removeCls = function(el, cls) {
		if(!u.isElement(el)) {
			console.warn('$api.removeCls Function need el param, el param must be DOM Element');
			return;
		}
		if('classList' in el) {
			el.classList.remove(cls);
		} else {
			var preCls = el.className;
			var newCls = preCls.replace(cls, '');
			el.className = newCls;
		}
		return el;
	};
	u.toggleCls = function(el, cls) {
		if(!u.isElement(el)) {
			console.warn('$api.toggleCls Function need el param, el param must be DOM Element');
			return;
		}
		if('classList' in el) {
			el.classList.toggle(cls);
		} else {
			if(u.hasCls(el, cls)) {
				u.removeCls(el, cls);
			} else {
				u.addCls(el, cls);
			}
		}
		return el;
	};
	u.val = function(el, val) {
		if(!u.isElement(el)) {
			console.warn('$api.val Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length === 1) {
			switch(el.tagName) {
				case 'SELECT':
					var value = el.options[el.selectedIndex].value;
					return value;
					break;
				case 'INPUT':
					return el.value;
					break;
				case 'TEXTAREA':
					return el.value;
					break;
			}
		}
		if(arguments.length === 2) {
			switch(el.tagName) {
				case 'SELECT':
					el.options[el.selectedIndex].value = val;
					return el;
					break;
				case 'INPUT':
					el.value = val;
					return el;
					break;
				case 'TEXTAREA':
					el.value = val;
					return el;
					break;
			}
		}

	};
	u.prepend = function(el, html) {
		if(!u.isElement(el)) {
			console.warn('$api.prepend Function need el param, el param must be DOM Element');
			return;
		}
		el.insertAdjacentHTML('afterbegin', html);
		return el;
	};
	u.append = function(el, html) {
		if(!u.isElement(el)) {
			console.warn('$api.append Function need el param, el param must be DOM Element');
			return;
		}
		el.insertAdjacentHTML('beforeend', html);
		return el;
	};
	u.before = function(el, html) {
		if(!u.isElement(el)) {
			console.warn('$api.before Function need el param, el param must be DOM Element');
			return;
		}
		el.insertAdjacentHTML('beforebegin', html);
		return el;
	};
	u.after = function(el, html) {
		if(!u.isElement(el)) {
			console.warn('$api.after Function need el param, el param must be DOM Element');
			return;
		}
		el.insertAdjacentHTML('afterend', html);
		return el;
	};
	u.html = function(el, html) {
		if(!u.isElement(el)) {
			console.warn('$api.html Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length === 1) {
			return el.innerHTML;
		} else if(arguments.length === 2) {
			el.innerHTML = html;
			return el;
		}
	};
	u.text = function(el, txt) {
		if(!u.isElement(el)) {
			console.warn('$api.text Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length === 1) {
			return el.textContent;
		} else if(arguments.length === 2) {
			el.textContent = txt;
			return el;
		}
	};
	u.offset = function(el) {
		if(!u.isElement(el)) {
			console.warn('$api.offset Function need el param, el param must be DOM Element');
			return;
		}
		var sl = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		var st = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

		var rect = el.getBoundingClientRect();
		return {
			l: rect.left + sl,
			t: rect.top + st,
			w: el.offsetWidth,
			h: el.offsetHeight
		};
	};
	u.css = function(el, css) {
		if(!u.isElement(el)) {
			console.warn('$api.css Function need el param, el param must be DOM Element');
			return;
		}
		if(typeof css == 'string' && css.indexOf(':') > 0) {
			el.style && (el.style.cssText += ';' + css);
		}
	};
	u.cssVal = function(el, prop) {
		if(!u.isElement(el)) {
			console.warn('$api.cssVal Function need el param, el param must be DOM Element');
			return;
		}
		if(arguments.length === 2) {
			var computedStyle = window.getComputedStyle(el, null);
			return computedStyle.getPropertyValue(prop);
		}
	};
	u.jsonToStr = function(json) {
		if(typeof json === 'object') {
			return JSON && JSON.stringify(json);
		}
	};
	u.loadJsonToForm = function(json) {
		var key, value, tagName, type, arr;
		for(x in json) {
			key = x;
			value = json[x];
			$("#" + key).each(function() {
				tagName = $(this)[0].tagName;
				type = $(this).attr('type');
				if(tagName == 'INPUT') {
					if(type == 'radio') {
						$(this).attr('checked', $(this).val() == value);
					} else if(type == 'checkbox') {
						arr = value.split(',');
						for(var i = 0; i < arr.length; i++) {
							if($(this).val() == arr[i]) {
								$(this).attr('checked', true);
								break;
							}
						}
					} else {
						$(this).val(value);
					}
				} else if(tagName == 'TEXTAREA') {
					$(this).val(value);
				}

			});
		}

	};
	u.strToJson = function(str) {
		if(typeof str === 'string') {
			return JSON && JSON.parse(str);
		}
	};
	u.setStorage = function(key, value) {
		if(arguments.length === 2) {
			var v = value;
			if(typeof v == 'object') {
				v = JSON.stringify(v);
				v = 'obj-' + v;
			} else {
				v = 'str-' + v;
			}
			var ls = uzStorage();
			if(ls) {
				ls.setItem(key, v);
			}
		}
	};
	u.getStorage = function(key) {
		var ls = uzStorage();
		if(ls) {
			var v = ls.getItem(key);
			if(!v) {
				return;
			}
			if(v.indexOf('obj-') === 0) {
				v = v.slice(4);
				return JSON.parse(v);
			} else if(v.indexOf('str-') === 0) {
				return v.slice(4);
			}
		}
	};
	u.rmStorage = function(key) {
		var ls = uzStorage();
		if(ls && key) {
			ls.removeItem(key);
		}
	};
	u.clearStorage = function() {
		var ls = uzStorage();
		if(ls) {
			ls.clear();
		}
	};

	u.getNow = function() {
		var myDate = new Date();
		var now = myDate.getYear() + "-" + myDate.getMonth() + "-" + myDate.getDate();
		return now;
	};
	u.dataFormat = function(timespan) {
		var dateTime = new Date(timespan);
		var year = dateTime.getFullYear();
		var month = dateTime.getMonth() + 1;
		var day = dateTime.getDate();
		var hour = dateTime.getHours();
		var minute = dateTime.getMinutes();
		var second = dateTime.getSeconds();
		// var now = new Date();
		var now_new = Math.round(new Date().getTime() / 1000); //typescript转换写法

		var milliseconds = 0;
		var timeSpanStr;

		milliseconds = now_new - timespan;

		if(milliseconds <= 1000 * 60 * 1) {
			timeSpanStr = '刚刚';
		} else if(1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
		} else if(1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
		} else if(1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
		} else if(milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
			timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
		} else {
			timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
		}
		return timeSpanStr;

	};
	/*by king*/
	u.fixIos7Bar = function(el) {
		if(!u.isElement(el)) {
			console.warn('$api.fixIos7Bar Function need el param, el param must be DOM Element');
			return;
		}
		var strDM = api.systemType;
		if(strDM == 'ios') {
			var strSV = api.systemVersion;
			var numSV = parseInt(strSV, 10);
			var fullScreen = api.fullScreen;
			var iOS7StatusBarAppearance = api.iOS7StatusBarAppearance;
			if(numSV >= 7 && !fullScreen && iOS7StatusBarAppearance) {
				el.style.paddingTop = '20px';
			}
		}
	};

	u.getLimit = function(mid, isSys, callbacksucc) {
		var url = BaseUrl + "SysRoleMenuDetail?token=" + u.getStorage("token") + "&mid=" + mid;
		if(isSys == 0) {
			url = BaseUrl + "RoleMenuDetail?token=" + u.getStorage("token") + "&mid=" + mid;
		}
		$.getJSON(url, function(data) {

			var items = "";
			if(u.isEmpty(data)) return;
			for(var i = 0; i < data.Rows.length; i++) {
				var item = data.Rows[i];

				if("add" == item.MHander)
					items += "<li><a class='add'   onclick='add();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("edit" == item.MHander)
					items += "<li><a class='all'   onclick='edit();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("del" == item.MHander)
					items += "<li><a class='del'   onclick='del();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("down" == item.MHander)
					items += "<li><a class='pdown'   onclick='down();'><i></i><span>下架</span></a></li>";
				else if("pup" == item.MHander)
					items += "<li><a class='pup'   onclick='up();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("up" == item.MHander)
					items += "<li><a class='pup'   onclick='up();'><i></i><span>上架</span></a></li>";
				else if("addM" == item.MHander)
					items += "<li><a class='add'   onclick='addM();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("addS" == item.MHander)
					items += "<li><a class='add'   onclick='addS();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("addT" == item.MHander)
					items += "<li><a class='add'   onclick='addT();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("addP" == item.MHander)
					items += "<li><a class='add'   onclick='addP();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("move" == item.MHander)
					items += "<li><a class='move'   onclick='move();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("audit" == item.MHander)
					items += "<li><a class='audit'   onclick='audit();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("import" == item.MHander)
					items += "<li><a class='import'   onclick='import2();'><i></i><span>" + item.Name + "</span></a></li>";
				else if("export" == item.MHander)
					items += "<li><a class='export'   onclick='export2();'><i></i><span>" + item.Name + "</span></a></li>";

			}
			callbacksucc(items);
		});

	};

	u.fixStatusBar = function(el) {
		if(!u.isElement(el)) {
			console.warn('$api.fixStatusBar Function need el param, el param must be DOM Element');
			return;
		}
		var sysType = api.systemType;
		if(sysType == 'ios') {
			u.fixIos7Bar(el);
		} else if(sysType == 'android') {
			var ver = api.systemVersion;
			ver = parseFloat(ver);
			if(ver >= 4.4) {
				el.style.paddingTop = '25px';
			}
		}
	};
	u.showDialog = function(msg) {
		$("#all").css("display", "block");
		$("#loading").busyLoad("show", {
			background: "rgba(153, 153, 153, 1)",
			text: msg,
			textPosition: "bottom",
			fontSize: "14px",
			animation: "fade",
			containerClass: "loading",
			image: ""
			//""
		});
	};
	u.hideDialog = function() {
		$("#loading").busyLoad("hide", {
			animation: "fade"
		});
		$("#all").css("display", "none");
	};
	u.showtoast = function(html) {
		var obj = $(".toast");
		if(obj.length == 0) {
			$('<div    class="toast" style="display: none; z-index:99999;opacity: 0;"><div id="toast" style="z-index:99999;top:100px;color:#fff;font-size:14px;background: rgba(0, 0, 0, 0.6);border-radius: 2px;padding: 2px;text-align: center;line-height:32px;width:275px;margin: -200px auto;"></div></div>')
				.appendTo($('body'));
		}
		$(".toast").css("display", "block");
		$(".toast").css("opacity", 1);
		$("#toast").text(html);
		var toastTime2 = setTimeout(function() {
			$(".toast").css("opacity", 0);
			var displayTime2 = setTimeout(function() {
				$(".toast").css("display", "none");
			}, 2000);
		}, 2000);
	};
	u.showLToast = function(html, tm) {
		var obj = $(".toast");
		if(obj.length == 0) {
			$('<div  class="toast" style="display: none; z-index:99999;opacity: 0;"><div id="toast" style="color:#fff;font-size:14px;background: rgba(0, 0, 0, 0.6);border-radius: 2px;padding: 2px;position:relative;text-align: center;line-height:32px;width:275px;margin: -200 auto;"></div></div>')
				.appendTo($('body'));
		}
		$(".toast").css("display", "block");
		$(".toast").css("opacity", 1);
		$("#toast").html(html);
		var toastTime2 = setTimeout(function() {
			$(".toast").css("opacity", 0);
			$(".toast").css("display", "none");
			$(".toast").remove();
		}, tm);
	};
	u.toast = function(title, text, time) {
		var opts = {};
		var show = function(opts, time) {
			api.showProgress(opts);
			setTimeout(function() {
				api.hideProgress();
			}, time);
		};
		if(arguments.length === 1) {
			var time = time || 500;
			if(typeof title === 'number') {
				time = title;
			} else {
				opts.title = title + '';
			}
			show(opts, time);
		} else if(arguments.length === 2) {
			var time = time || 500;
			var text = text;
			if(typeof text === "number") {
				var tmp = text;
				time = tmp;
				text = null;
			}
			if(title) {
				opts.title = title;
			}
			if(text) {
				opts.text = text;
			}
			show(opts, time);
		}
		if(title) {
			opts.title = title;
		}
		if(text) {
			opts.text = text;
		}
		time = time || 500;
		show(opts, time);
	};
	u.post = function( /*url,data,fnSuc,dataType*/ ) {
		var argsToJson = parseArguments.apply(null, arguments);
		var json = {};
		var fnSuc = argsToJson.fnSuc;
		argsToJson.url && (json.url = argsToJson.url);
		argsToJson.data && (json.data = argsToJson.data);
		if(argsToJson.dataType) {
			var type = argsToJson.dataType.toLowerCase();
			if(type == 'text' || type == 'json') {
				json.dataType = type;
			}
		} else {
			json.dataType = 'json';
		}
		json.method = 'post';
		api.ajax(json,
			function(ret, err) {
				if(ret) {
					fnSuc && fnSuc(ret);
				}
			}
		);
	};

	u.showProgress = function(target) {
		var opts = {
			lines: 9, // The number of lines to draw
			length: 16, // The length of each line
			width: 13, // The line thickness
			radius: 26, // The radius of the inner circle
			corners: 1, // Corner roundness (0..1)
			rotate: 84, // The rotation offset
			color: '#000', // #rgb or #rrggbb
			speed: 1.5, // Rounds per second
			trail: 41, // Afterglow percentage
			shadow: false, // Whether to render a shadow
			hwaccel: false, // Whether to use hardware acceleration
			className: 'spinner', // The CSS class to assign to the spinner
			zIndex: 2e9, // The z-index (defaults to 2000000000)
			top: 'auto', // Top position relative to parent in px
			left: 'auto' // Left position relative to parent in px
		};
		var spinner = new Spinner(opts).spin(target);

	};
	u.get = function( /*url,fnSuc,dataType*/ ) {
		var argsToJson = parseArguments.apply(null, arguments);
		var json = {};
		var fnSuc = argsToJson.fnSuc;
		argsToJson.url && (json.url = argsToJson.url);
		//argsToJson.data && (json.data = argsToJson.data);
		if(argsToJson.dataType) {
			var type = argsToJson.dataType.toLowerCase();
			if(type == 'text' || type == 'json') {
				json.dataType = type;
			}
		} else {
			json.dataType = 'text';
		}
		json.method = 'get';
		u.ajax(json,
			function(ret, err) {
				if(ret) {
					fnSuc && fnSuc(ret);
				}
			}
		);
	};

	/*end*/

	window.$api = u;

})(window);